package org.domeos.framework.api.controller.deployment;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.domeos.basemodel.HttpResponseTemp;
import org.domeos.basemodel.ResultStat;
import org.domeos.framework.api.consolemodel.deployment.DeploymentDraft;
import org.domeos.framework.api.controller.common.BaseSreController;
import org.domeos.framework.api.model.deployment.Policy;
import org.domeos.framework.api.model.deployment.related.LoadBalancerForDeployDraft;
import org.domeos.framework.api.service.deployment.DeploymentService;
import org.domeos.framework.api.service.deployment.InstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sre/api/deploy")
@Api("SRE部署相关API")
public class DeploymentSreController extends BaseSreController {

	@Autowired
	DeploymentService deploymentService;
	
	@Autowired
    InstanceService instanceService;

	@ApiOperation(value = "部署实例检查")
	@ResponseBody
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	public HttpResponseTemp<?> checkDeployment(HttpServletRequest request,
			@RequestBody DeploymentDraft deploymentDraft) throws Exception {
		if (StringUtils.isEmpty(deploymentDraft.getDeployName())) {
			return ResultStat.PARAM_ERROR.wrap("deployment name is null!");
		}
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		deploymentDraft.setUserId(getIntValue(userId));
		return deploymentService.sreCheckDeployment(deploymentDraft);
	}

	@ApiOperation(value = "部署实例创建")
	@ResponseBody
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public HttpResponseTemp<?> createDeployment(HttpServletRequest request,
			@RequestBody DeploymentDraft deploymentDraft) throws Exception {
		if (StringUtils.isEmpty(deploymentDraft.getDeployName())) {
			return ResultStat.PARAM_ERROR.wrap("deployment name is null!");
		}
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		deploymentDraft.setUserId(getIntValue(userId));
		return deploymentService.sreCreateDeployment(deploymentDraft);
	}

	@ApiOperation(value = "启动实例")
	@ResponseBody
	@RequestMapping(value = "/action/start", method = RequestMethod.POST)
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deployId" , value = "服务ID" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "versionId" , value = "版本号" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "replicas" , value = "副本数量")
	})
	public HttpResponseTemp<?> startDeployment(HttpServletRequest request,
			@RequestParam(value = "deployId", required = true) int deployId,
			@RequestParam(value = "versionId", required = true) int versionId,
			@RequestParam(value = "replicas", required = false, defaultValue = "-1") int replicas)
			throws Exception {
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		return deploymentService.sreStartDeployment(deployId, versionId, replicas ,getIntValue(userId));
	}

	@ApiOperation(value = "终止实例")
	@ResponseBody
	@RequestMapping(value = "/action/abort", method = RequestMethod.POST)
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deployId" , value = "服务ID" , dataType = "int" , required = true)
	})
	public HttpResponseTemp<?> abortDeployOperation(HttpServletRequest request,
			@RequestParam(value = "deployId", required = true) int deployId)
			throws Exception {
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		return deploymentService.sreAbortDeployOperation(deployId,getIntValue(userId));
	}

	@ApiOperation(value = "停止实例")
	@ResponseBody
	@RequestMapping(value = "/action/stop", method = RequestMethod.POST)
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deployId" , value = "服务ID" , dataType = "int" , required = true)
	})
	public HttpResponseTemp<?> stopDeployment(HttpServletRequest request, @RequestParam int deployId)
			throws Exception {
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		return deploymentService.sreStopDeployment(deployId,getIntValue(userId));
	}

	@ApiOperation(value = "回滚实例")
	@ResponseBody
	@RequestMapping(value = "/action/rollback", method = RequestMethod.POST)
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deployId" , value = "服务ID" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "versionId" , value = "版本号" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "replicas" , value = "副本数量")
	})
	public HttpResponseTemp<?> startRollback(HttpServletRequest request,
			@RequestParam(value = "deployId", required = true) int deployId,
			@RequestParam(value = "versionId", required = true) int versionId,
			@RequestParam(value = "replicas", required = false, defaultValue = "-1") int replicas,
			@RequestBody(required = false) Policy policy) throws Exception {
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		return deploymentService.sreStartRollback(deployId, versionId, replicas,policy,getIntValue(userId));
	}

	@ApiOperation(value = "扩容实例")
	@ResponseBody
	@RequestMapping(value = "/action/scaleup", method = RequestMethod.POST)
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deployId" , value = "服务ID" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "versionId" , value = "版本号" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "replicas" , value = "副本数量")
	})
	public HttpResponseTemp<?> scaleupDeployment(HttpServletRequest request,
			@RequestParam(value = "deployId", required = true) int deployId,
			@RequestParam(value = "versionId", required = true) int versionId,
			@RequestParam(value = "replicas", required = false, defaultValue = "-1") int replicas)
			throws Exception {
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		return deploymentService.sreScaleUpDeployment(deployId, versionId, replicas ,getIntValue(userId));
	}

	@ApiOperation(value = "缩容实例")
	@ResponseBody
	@RequestMapping(value = "/action/scaledown", method = RequestMethod.POST)
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deployId" , value = "服务ID" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "versionId" , value = "版本号" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "replicas" , value = "副本数量")
	})
	public HttpResponseTemp<?> scaledownDeployment(HttpServletRequest request,
			@RequestParam(value = "deployId", required = true) int deployId,
			@RequestParam(value = "versionId", required = true) int versionId,
			@RequestParam(value = "replicas", required = false, defaultValue = "-1") int replicas)
			throws Exception {
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		return deploymentService.sreScaleDownDeployment(deployId, versionId, replicas ,getIntValue(userId));
	}
	
	@ApiOperation(value = "重启deploy")
	@ResponseBody
	@RequestMapping(value = "/action/restart", method = RequestMethod.GET)
	@ApiImplicitParams(@ApiImplicitParam(name = "deployId" , value = "服务ID" , dataType = "int" , required = true))
	public HttpResponseTemp<?> restartDeployment(HttpServletRequest request,
			@RequestParam(value = "deployId", required = true) int deployId,
			@RequestParam(value = "period", required = false,defaultValue="3000") int period,
			@RequestParam(value = "percent", required = false,defaultValue="0.5") double percent)
			throws Exception {
		
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		return deploymentService.sreRestartDeployment(deployId,period,percent,getIntValue(userId));
	}
	
	
	
	@ApiOperation(value="更新实例")
    @ResponseBody
    @RequestMapping(value = "/action/update", method = RequestMethod.POST)
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deployId" , value = "服务ID" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "versionId" , value = "版本号" , dataType = "int" , required = true),
		@ApiImplicitParam(name = "replicas" , value = "副本数量")
	})
    public HttpResponseTemp<?> startUpdate(HttpServletRequest request, @RequestParam(value = "deployId", required = true) int deployId,
                                           @RequestParam(value = "versionId", required = true) int versionId,
                                           @RequestParam(value = "replicas", required = false, defaultValue = "-1") int replicas,
                                           @RequestBody(required = false) Policy policy)
            throws Exception {
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
        return deploymentService.sreStartUpdate(deployId, versionId, replicas, policy ,getIntValue(userId));
    }
	
	@ApiOperation(value="获取部署实例状态")
    @ResponseBody
    @RequestMapping(value = "/{id}/instance", method = RequestMethod.GET)
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deployId" , value = "服务ID" , dataType = "int" , required = true)
	})
    public HttpResponseTemp<?> listPodsByDeployId(HttpServletRequest request, @RequestParam(value = "deployId", required = true) int deployId) throws Exception {
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		return deploymentService.sreListPodsByDeployId(deployId , getIntValue(userId));
    }
	
	@ApiOperation(value="获取部署实例事件")
    @ResponseBody
    @RequestMapping(value = "/event/list", method = RequestMethod.GET)
    public HttpResponseTemp<?> listDeployEvent(@RequestParam(value = "deployId", required = true) int deployId)
            throws Exception {
        return deploymentService.sreListDeployEvent(deployId);
    }
	
	@ApiOperation(value="修改service 负载均衡")
    @ResponseBody
    @RequestMapping(value = "/id/{deployId}/{lbname}/loadbalancer", method = RequestMethod.PUT)
    public HttpResponseTemp<?> modifyInnerService(HttpServletRequest request,@PathVariable int deployId,
    												@PathVariable String lbname,
                                                     @RequestBody LoadBalancerForDeployDraft loadBalancerDraft) throws Exception {
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		return deploymentService.sreModifyInnerService(deployId, lbname,loadBalancerDraft,getIntValue(userId));
    }
	
	@ApiOperation(value="获取service 负载均衡")
    @ResponseBody
    @RequestMapping(value = "/id/{deployId}/loadbalancer", method = RequestMethod.GET)
    public HttpResponseTemp<?> listLoadBalancer(@PathVariable int deployId) throws Exception {
        return deploymentService.sreListLoadBalancer(deployId);
    }

}
